package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.STORE_SYS;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.Promote;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.PromoteMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class PromoteFroAct {
	/**
	 * 店铺限时打折列表页
	 */
	private static final String TPL_PROMOTE_STORE_LIST="tpl.promoteStoreList";
	/**
	 * 店铺限时打折添加页
	 */
	private static final String TPL_PROMOTE_STORE_ADD="tpl.promoteStoreAdd";
	/**
	 * 店铺限时打折修改页
	 */
	private static final String TPL_PROMOTE_STORE_EDIT="tpl.promoteStoreEdit";
	
	// 店铺限时打折管理
	@RequestMapping(value = "/promoteFro/list*.jspx")
	public String list(String cl,Integer pageNo,HttpServletRequest request,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		model.addAttribute("cl", cl);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_PROMOTE_STORE_LIST), request);
	}
	
	// 店铺限时打折添加
	@RequestMapping(value = "/promoteFro/add.jspx")
	public String add(HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_PROMOTE_STORE_ADD), request);
	}
	
	// 店铺限时打折保存
	@RequestMapping(value = "/promoteFro/save.jspx")
	public String save(Promote bean,HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		manager.save(bean,web,store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_PROMOTE_STORE_ADD), request);
	}
	
	// 店铺限时打折修改
	@RequestMapping(value = "/promoteFro/edit.jspx")
	public String edit(Long id,Integer pageNo,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Promote promote=manager.findById(id);
		model.addAttribute("promote", promote);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_PROMOTE_STORE_EDIT), request);
	}
	
	// 店铺限时打折更新
	@RequestMapping(value = "/promoteFro/update.jspx", method = RequestMethod.POST)
	public String update(Promote bean,Integer pageNo,HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		manager.update(bean);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
//		return list(pageNo,request,model);
		return "redirect:promoteFro/list*.jspx";
	}
	
	//广告版位删除
	@RequestMapping("/promoteFro/delete.jspx")
	public String delete(Long id, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
//		Promote bean=manager.findById(id);
//		manager.update(bean);
		manager.deleteById(id);
//		return list(pageNo, request, model);
		return "redirect:promoteFro/list*.jspx";
	}
	

	
	@Autowired
	private PromoteMng manager;
}
